//
//  FetchProductDetailRequest.swift
//  Nahuo
//
//  Created by zang qilong on 2017/5/20.
//  Copyright © 2017年 zang qilong. All rights reserved.
//

import UIKit
import YTKNetwork
import SwiftyJSON

class FetchProductDetailRequest: YTKRequest {
    var productID:String?
    
    init(id:String) {
        productID = id
    }
    
    override func requestUrl() -> String {
        return "/product/view"
    }
    
    override func requestMethod() -> YTKRequestMethod {
        return .GET
    }
    
    override func requestHeaderFieldValueDictionary() -> [String : String]? {
        
        return NaHuoHttpUtil.sharedInstance.generateVerifyMobileHeader()
    }
    
    override func requestArgument() -> Any? {
        if let id = productID {
            return ["id": id];
        }
        return nil;
    }
    
    func fetchProductDetail(complection:@escaping ((Product) -> Void)) {
        self.startWithCompletionBlock(success: { (request) in
            let json = JSON(request.responseObject as Any)
            guard let code = json["code"].int else {
                printCodeError(request: request as! YTKRequest)
                return
            }
            
            guard code == 0 else {
                return
            }
            if let data = json["data"].dictionaryObject {

                if let product = Product.yy_model(withJSON: data) {
                    complection(product)
                }
                
            }
        }) { (request) in
            
        }
    }

}
